﻿using SlamDunk.MES.Domain.Entities.DefinitionContext;
using SlamDunk.MES.Domain.IDomainServices;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SlamDunk.MES.DomainService.Implements
{
    /// <summary>
    /// 物料清单领域服务实现
    /// </summary>
    public class BomService : IBomService
    {
        #region # 计算物料需求 —— IDictionary<Guid, decimal> CalculateMaterialAsks(Bom bom...
        /// <summary>
        /// 计算物料需求
        /// </summary>
        /// <param name="bom">物料清单</param>
        /// <param name="runSize">运行规模</param>
        /// <returns>物料需求列表</returns>
        public IDictionary<Guid, decimal> CalculateMaterialAsks(Bom bom, decimal runSize)
        {
            IDictionary<Guid, decimal> itemRequiredQuantities = new Dictionary<Guid, decimal>();
            foreach (BomItem bomItem in bom.Items.Where(x => x.DirectionType == MaterialDirectionType.Input).OrderBy(x => x.Sort))
            {
                if (itemRequiredQuantities.ContainsKey(bomItem.MaterialId))
                {
                    itemRequiredQuantities[bomItem.MaterialId] += (bomItem.Ratio * runSize);
                }
                else
                {
                    itemRequiredQuantities[bomItem.MaterialId] = (bomItem.Ratio * runSize);
                }
            }

            return itemRequiredQuantities;
        }
        #endregion
    }
}
